package com.hospital.controller;

import com.hospital.common.Result;
import com.hospital.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件上传控制器
 *
 * @author Hospital Management System
 */
@Api(tags = "文件管理")
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {

    private final FileService fileService;

    @ApiOperation("上传单个文件")
    @PostMapping("/upload")
    public Result<String> uploadFile(
            @ApiParam("上传的文件") @RequestParam("file") MultipartFile file,
            @ApiParam("文件类型：avatar-头像, document-文档, image-图片, report-报告") 
            @RequestParam(value = "type", defaultValue = "image") String type) {
        
        String fileUrl = fileService.uploadFile(file, type);
        return Result.success(fileUrl, "文件上传成功");
    }

    @ApiOperation("上传多个文件")
    @PostMapping("/upload/batch")
    public Result<List<String>> uploadFiles(
            @ApiParam("上传的文件列表") @RequestParam("files") MultipartFile[] files,
            @ApiParam("文件类型") @RequestParam(value = "type", defaultValue = "image") String type) {
        
        List<String> fileUrls = fileService.uploadFiles(files, type);
        return Result.success(fileUrls, "文件批量上传成功");
    }

    @ApiOperation("上传头像")
    @PostMapping("/upload/avatar")
    public Result<String> uploadAvatar(@ApiParam("头像文件") @RequestParam("file") MultipartFile file) {
        String fileUrl = fileService.uploadFile(file, "avatar");
        return Result.success(fileUrl, "头像上传成功");
    }

    @ApiOperation("上传医疗报告")
    @PostMapping("/upload/report")
    public Result<String> uploadReport(@ApiParam("报告文件") @RequestParam("file") MultipartFile file) {
        String fileUrl = fileService.uploadFile(file, "report");
        return Result.success(fileUrl, "报告上传成功");
    }

    @ApiOperation("上传处方图片")
    @PostMapping("/upload/prescription")
    public Result<String> uploadPrescriptionImage(@ApiParam("处方图片") @RequestParam("file") MultipartFile file) {
        String fileUrl = fileService.uploadFile(file, "prescription");
        return Result.success(fileUrl, "处方图片上传成功");
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/delete")
    public Result<Void> deleteFile(@ApiParam("文件URL") @RequestParam("fileUrl") String fileUrl) {
        fileService.deleteFile(fileUrl);
        return Result.success(null, "文件删除成功");
    }

    @ApiOperation("获取文件信息")
    @GetMapping("/info")
    public Result<Object> getFileInfo(@ApiParam("文件URL") @RequestParam("fileUrl") String fileUrl) {
        Object fileInfo = fileService.getFileInfo(fileUrl);
        return Result.success(fileInfo, "获取文件信息成功");
    }
} 